#import "dfhgwhruebvhrheymournrural.h"
#import "dfhgwhruebvhrheysternideology.h"
#import "dfhgwhruebvhrheyluggagerecorder.h"
#import "dfhgwhruebvhrheythrivealternate.h"
#import "dfhgwhruebvhrheyindependenceyearlyViewController.h"
@interface dfhgwhruebvhrheyindependenceyearlyViewController ()
@property(nonatomic, strong) dfhgwhruebvhrheymournrural *timer;
@end
@implementation dfhgwhruebvhrheyindependenceyearlyViewController {
    int cut_down;
    UIView *bgView;
    UILabel *textLabel;
    UIButton *sendBtn;
    UILabel *codeText;
    dfhgwhruebvhrheythrivealternate *code;
    UILabel *passwdText;
    dfhgwhruebvhrheythrivealternate *passwd;
    UIButton *confirmBtn;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inputTextChange:) name:UITextFieldTextDidChangeNotification object:nil];
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}
- (dfhgwhruebvhrheymournrural *)timer {
    if (_timer == nil) {
        _timer = [[dfhgwhruebvhrheymournrural alloc] init];
    }
    return _timer;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadUI];
    [self sendRegSms];
}
- (void)loadUI {
    cut_down = dhgfywegwygeydwleakconceal;
    [self configBackIcon:@"dhgfywegwygeydwcurvereciprocal"];
    self.navigationItem.title = @"Forgot password";
    self.navigationStyle = dhgfywegwygeydwreciprocaltransport;
    bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    textLabel = [[UILabel alloc] init];
    textLabel.font = [UIFont regular:15];
    textLabel.numberOfLines = 0;
    textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    textLabel.textColor = text_color4.dhgfywegwygeydwlearneddesignate;
    [bgView addSubview:textLabel];
    if (![self.mobile hasPrefix:@"0"]) {
        self.mobile = [NSString stringWithFormat:@"0%@",self.mobile];
    }
    NSString *mobile = [self.mobile stringByReplacingCharactersInRange:NSMakeRange(4, 4) withString:@"****"];
    textLabel.text = [NSString stringWithFormat:@"Your verification code is sent via SMS to: %@", mobile];
    sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sendBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
    sendBtn.frame = CGRectMake(0, 0, 70, 40);
    sendBtn.tag = 100;
    sendBtn.layer.cornerRadius = 8;
    [sendBtn setTitle:@"Send" forState:UIControlStateNormal];
    [sendBtn addTarget:self action:@selector(btnActions:) forControlEvents:UIControlEventTouchUpInside];
    codeText = [[UILabel alloc] init];
    codeText.text = @"Verification code";
    codeText.font = [UIFont regular:16];
    codeText.textColor = text_color1.dhgfywegwygeydwlearneddesignate;
    [bgView addSubview:codeText];
    code = [[dfhgwhruebvhrheythrivealternate alloc] init];
    [bgView addSubview:code];
    code.tag = 100;
    code.rightBtn = sendBtn;
    code.placeholder = @"A 4-digit OTP";
    [bgView addSubview:code];
    passwdText = [[UILabel alloc] init];
    passwdText.text = @"Password";
    passwdText.font = [UIFont regular:16];
    passwdText.textColor = text_color1.dhgfywegwygeydwlearneddesignate;
    [bgView addSubview:passwdText];
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.tag = 101;
    rightBtn.frame = CGRectMake(0, 0, 24, 24);
    [rightBtn setImage:[UIImage imageNamed:@"dhgfywegwygeydwreceiptaffirm"] forState:UIControlStateSelected];
    [rightBtn setImage:[UIImage imageNamed:@"dhgfywegwygeydwaffirmsalesman"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(btnActions:) forControlEvents:UIControlEventTouchUpInside];
    passwd = [[dfhgwhruebvhrheythrivealternate alloc] init];
    [bgView addSubview:passwd];
    passwd.rightBtn = rightBtn;
    passwd.tag = 101;
    passwd.secureTextEntry = YES;
    passwd.placeholder = @"Please enter a 6-digit password";
    confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmBtn.tag = 102;
    confirmBtn.enabled = NO;
    confirmBtn.layer.cornerRadius = 10;
    confirmBtn.titleLabel.font = [UIFont medium:16];
    [confirmBtn setTitle:@"Confirm" forState:UIControlStateNormal];
    confirmBtn.backgroundColor = [primary_color dhgfywegwygeydwlearneddesignate:0.4];
    [confirmBtn setTitleColor:white_color.dhgfywegwygeydwlearneddesignate forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(btnActions:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:confirmBtn];
}
- (void)changePassword {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"code"] = code.content;
    params[@"mobile"] = self.mobile;
    params[@"password"] = passwd.content;
    params[@"pgid"] = @"1";
    params[@"type"] = @"2";
    [dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
    __weak typeof(self) weakSelf = self;
    [[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwawaitlamb path:resetPasswordApi isEncrypt:YES withParams:params success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
        [dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
        __strong typeof(weakSelf) strongSelf = weakSelf;
        dfhgwhruebvhrheysternideology *model = [dfhgwhruebvhrheysternideology yy_modelWithJSON:json];
        if (model.code == 200) {
            if (strongSelf.isNeedSysMobile) {            
                [[NSUserDefaults standardUserDefaults] setObject:self.mobile forKey:dhgfywegwygeydwgratitudepreside];
            }
            [strongSelf.navigationController popToRootViewControllerAnimated:YES];
        }else {
            [dfhgwhruebvhrheyoptionsaving.keyWindow show:model.error];
        }
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        [dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
    }];
}
- (void)btnActions:(UIButton *)sender {
    switch (sender.tag) {
        case 100: {
            [self sendRegSms];
        }
            break;
        case 101: {
            sender.selected = !sender.selected;
            passwd.secureTextEntry = !sender.isSelected;
        }
            break;
        case 102: {
            if (code.content.length != 4) {
                return [self.view show:@"please enter a 4-digit code"];
            }
            if (passwd.content.length != 6) {
                return [self.view show:@"Enter 6-digit password"];
            }
            [self changePassword];
        }
            break;
        default:
            break;
    }
}
- (void)startTimer {
    sendBtn.enabled = NO;
    __weak typeof(self)weakSelf = self;
    sendBtn.backgroundColor = [primary_color dhgfywegwygeydwlearneddesignate:0.4];
    [self.timer startWithAction:^{
        __strong typeof(weakSelf)strongSelf = weakSelf;
        dispatch_async(dispatch_get_main_queue(), ^{
            [strongSelf updateCutTime];
        });
    }];
}
- (void)updateCutTime {
    if (cut_down <= 1) {
        cut_down = dhgfywegwygeydwleakconceal;
        [_timer cancel];
        _timer = nil;
        sendBtn.enabled = YES;
        [sendBtn setTitle:@"Send" forState:UIControlStateNormal];
        sendBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
    }else {
        cut_down -= 1;
        [sendBtn setTitle:[NSString stringWithFormat:@"%02ds", cut_down] forState:UIControlStateNormal];
    }
}
- (void)sendRegSms {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"mobile"] = self.mobile;
    params[@"pgid"] = @"1";
    [dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
    __weak typeof(self) weakSelf = self;
    [[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwawaitlamb path:forgetPasswdCodeApi isEncrypt:YES withParams:params success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
        [dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
        __strong typeof(weakSelf) strongSelf = weakSelf;
        dfhgwhruebvhrheysternideology *model = [dfhgwhruebvhrheysternideology yy_modelWithJSON:json];
        if (model.code == 200) {
            [strongSelf startTimer];
        }else{
            [dfhgwhruebvhrheyoptionsaving.keyWindow show:model.error];
        }
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        [dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
    }];
}
- (void)inputTextChange:(NSNotification *)not {
    UITextField *input = [not object];
    NSString *text = input.text;
    if (input.superview.tag == 100) {
        if (text.length >= 4) {
            code.content = [text substringToIndex:4];
            if (passwd.content.length == 6) {
                confirmBtn.enabled = YES;
                confirmBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
            }else {
                confirmBtn.enabled = NO;
                confirmBtn.backgroundColor = [primary_color dhgfywegwygeydwlearneddesignate:0.4];
            }
        }else {
            code.content = input.text;
            confirmBtn.enabled = NO;
            confirmBtn.backgroundColor = [primary_color dhgfywegwygeydwlearneddesignate:0.4];
        }
    }else {
        if (text.length >= 6) {
            passwd.content = [text substringToIndex:6];
            if (code.content.length == 4) {
                confirmBtn.enabled = YES;
                confirmBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
            }else {
                confirmBtn.enabled = NO;
                confirmBtn.backgroundColor = [primary_color dhgfywegwygeydwlearneddesignate:0.4];
            }
        }else {
            passwd.content = input.text;
            confirmBtn.enabled = NO;
            confirmBtn.backgroundColor = [primary_color dhgfywegwygeydwlearneddesignate:0.4];
        }
    }
}
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        make.left.bottom.right.equalTo(self.view);
    }];
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bgView).offset(-60);
        make.left.equalTo(bgView).offset(12);
        make.top.equalTo(bgView).offset(24);
        make.height.mas_equalTo(46);
    }];
    [codeText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textLabel.mas_bottom).offset(24);
        make.left.centerX.equalTo(textLabel);
        make.height.mas_equalTo(24);
    }];
    [code mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(codeText.mas_bottom).offset(12);
        make.centerX.equalTo(bgView);
        make.left.equalTo(codeText);
        make.height.mas_equalTo(56);
    }];
    [passwdText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(code.mas_bottom).offset(24);
        make.left.centerX.equalTo(code);
        make.height.mas_equalTo(24);
    }];
    [passwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(passwdText.mas_bottom).offset(12);
        make.left.centerX.equalTo(passwdText);
        make.height.mas_equalTo(56);
    }];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(passwd.mas_bottom).offset(40);
        make.left.centerX.equalTo(passwd);
        make.height.mas_equalTo(56);
    }];
}
@end
